package com.paomi.client.activity.person;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hannesdorfmann.mosby3.mvp.MvpView;
import com.paomi.client.R;
import com.paomi.client.activity.person.View.SetNameView;
import com.paomi.client.activity.person.presenter.SetNamePre;
import com.paomi.client.base.BaseMvpActivity;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.utils.Util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;

public class SetNameActivity extends BaseMvpActivity<SetNameView, SetNamePre> implements SetNameView {
    @BindView(R.id.et_nickName)
    EditText et_nickName;
    @BindView(R.id.tv_clear)
    TextView tv_clear;
    @BindView(R.id.tv_save)
    TextView tv_save;
    @BindView(R.id.button_goback)
    TextView button_goback;

    @NonNull
    @Override
    public SetNamePre createPresenter() {
        return new SetNamePre();
    }

    @Override
    protected String setTitle() {
        return getResources().getString(R.string.setting_name);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
    }

    @OnClick(R.id.tv_clear)
    void clear() {
        et_nickName.setText("");
    }

    @OnClick(R.id.tv_save)
    void save() {
        if (et_nickName.getText().toString().trim().isEmpty()) {
            Util.toast(this, "请输入昵称");
            return;
        } else if (et_nickName.getText().length() < 4) {
            Util.toast(this, "请输入4-20位昵称");
            return;
        } else if (et_nickName.getText().length() > 20) {
            Util.toast(this, "昵称不能多于20个字~");
            return;
        }

        Map<String, String> map = new HashMap<>();
        map.put("name", et_nickName.getText().toString().trim());
        getPresenter().setUserInfo(map);
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_set_name;
    }

    @Override
    protected void initEvent() {
        tv_clear.setTypeface(iconfont);
        button_goback.setTypeface(iconfont);

        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + getResources().getString(R.string.back));

        String name = getIntent().getStringExtra("name");

        setEditTextInhibitInputSpaChat(et_nickName);
        if (name != null && !name.equals("")) {
            et_nickName.setText(name);
        }
        et_nickName.setSelection(et_nickName.getText().toString().trim().length());
    }

    @Override
    public void showChangeUserInfo(BaseJSON result) {
        getUserInfo().setUserName(et_nickName.getText().toString().trim());
        Intent intent = getIntent();
        intent.putExtra("nickName", et_nickName.getText().toString());
        setResult(Activity.RESULT_OK, intent);
        finish();
    }
    /**
     * 禁止EditText输入空格
     *
     * @param editText
     */
    public static void setEditTextInhibitInputSpaChat(EditText editText) {
        InputFilter filter_space = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (source.equals(" "))
                    return "";
                else
                    return null;
            }
        };
        InputFilter filter_speChat = new InputFilter() {
            @Override
            public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
                String speChat = "[`~!@#_$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）— +|{}【】‘；：”“’。，、？]";
                Pattern pattern = Pattern.compile(speChat);
                Matcher matcher = pattern.matcher(charSequence.toString());
                if (matcher.find()) return "";
                else return null;
            }
        };
        editText.setFilters(new InputFilter[]{filter_space, filter_speChat});
    }

}
